<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>订单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>

<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="table-search">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">渠道类型</label>
                            <div class="layui-input-inline">
                                <div id="serverGroup"></div>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">选择区服:</label>
                            <div class="layui-input-inline">
                                <select name="gameArea" class="layui-select" >
                                    <option value="">全部</option>
                                    <option v-for="(item,index) in services" :value="item.server_id">{{item.name}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline" data-name="channel">
                            <label class="layui-form-label">渠道商品类型:</label>
                            <div class="layui-input-inline">
                                <select class="layui-select" lay-filter="setGemValue">
                                    <option :value="option.type" v-for="option in channelOptions">{{option.name}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                        <label class="layui-form-label">内购商品:</label>
                            <div class="layui-input-inline">
                                <select name="gameGoods" class="layui-select"  lay-search>
                                    <option value="">选择内购商品</option>
                                    <option v-for="item in gameGoodsList" :value="item.id" lay-ignore="">{{item.name}} - {{item.detail}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">支付渠道:</label>
                            <div class="layui-input-inline">
                                <select name="channel" class="layui-select">
                                    <option value="">全部</option>
                                    <option v-for="(item,index) in payChannel" :value="item.id">{{item.name}}</option>

                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">我司订单号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="orderNo" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">平台订单号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="channelOrderNo" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">长ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="customerId" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">角色ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="roleId" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">邀请码</label>
                            <div class="layui-input-inline">
                                <input type="text" name="invite_code" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">订单状态</label>
                            <div class="layui-input-inline">
                                <select name="status" class="layui-select">
                                    <option value="">全部</option>
                                    <option value="0">待支付</option>
                                    <option value="1">已支付</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">代理渠道:</label>
                            <div class="layui-input-inline">
                                <select name="gamechannel" class="layui-select" lay-filter="gamechannel">
                                    <option value="">全部</option>
                                    <option v-for="(item,index) in channels" :value="item.id">{{item.name}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">包号列表</label>
                            <div class="layui-input-inline">
                                <div id="serverPackage"></div>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">推送状态</label>
                            <div class="layui-input-inline">
                                <select name="push_status" class="layui-select">
                                    <option value="">全部</option>
                                    <option value="0">待推送</option>
                                    <option value="1">已推送</option>
                                    <option value="9">金额不匹配</option>

                                    <option value="-1">推送失败</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">记录时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="createDateTimeBegin" id="createTime0"
                                       placeholder="开始时间" autocomplete="off" class="layui-input" />
                                <input type="text" name="createDateTimeEnd" id="createTime1"
                                       placeholder="截止时间" autocomplete="off" class="layui-input" />
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
                <div class="layui-form" lay-filter="pay-order-sta" auth="payOrder:sta">
                    <div class="layui-form-item">

                    <div class="layui-inline">
                        <label class="layui-form-label">充值统计: </label>
                        <div class="layui-input-inline">
                            <input type="text" name="payTimeBegin" id="payTimeBegin"
                                   placeholder="开始时间" autocomplete="off" class="layui-input" />
                        </div>
                        <div class="layui-input-inline">
                            <input type="text" name="payTimeEnd" id="payTimeEnd"
                                   placeholder="截止时间" autocomplete="off" class="layui-input" />
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">区服列表</label>
                            <div class="layui-input-inline" >
                                <div style="width: 136px;" id="gameAreas"></div>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">代理渠道:</label>
                            <div class="layui-input-inline">
                                <select name="gamechannel" class="layui-select" lay-filter="gamechannel1">
                                    <option value="">全部</option>
                                    <option v-for="(item,index) in channels" :value="item.id">{{item.name}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">包号列表</label>
                            <div class="layui-input-inline">
                                <div id="serverPackage1"></div>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">支付渠道:</label>
                            <div class="layui-input-inline">
                                <select name="channel" class="layui-select">
                                    <option value="">全部</option>
                                    <option v-for="(item,index) in payChannel" :value="item.id">{{item.name}}</option>

                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">邀请码</label>
                            <div class="layui-input-inline">
                                <input type="text" name="invite_code" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-input-inline" >
                            <input type="text" value="0" class="layui-input" :value="paySum"/>
                        </div>
                        <div class="layui-input-inline">
                            <button class="layui-btn layui-btn-normal layui-btn-sm" v-on:click="seePaySum">查看</button>
                        </div>
                    </div>
                </div>
                </div>
            </div>

        </fieldset>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>

</body>
</html>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm" auth="payOrder:export" lay-event="export">导出</button>
    </div>
</script>
<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" auth="payOrder:edit">标记退款</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="query" auth="payOrder:query">主动查询</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="push" auth="payOrder:push">推送物品</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="force-push" auth="payOrder:forcePush">强制重推</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="nopay-force-push" auth="payOrder:nopayForcePush">未付款强推</a>
</script>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script src="/xm-select-v1.2.4/xm-select.js"></script>

<script>
    var form,$,_this;
    var services = JSON.parse(localStorage.getItem('services'))

    var channels = getEnum('channel')
    var gameGoods = getEnum('gameGoods')
    var serverGroup = getEnum('serverGroup')
    var payChannel = getEnum('payChannel')
    var configgroup =getEnum('configgroup')

    new Vue({
        el: "#app",
        data: {
            services:services,
            channels:channels,
            channel:payChannel,
            packages:[], //所有的包
            packages1:[], //所有的包

            paySum:0,
            selectedChannel: null,
            gameGoodsList:[],
            channelOptions: configgroup,
        },
        methods:{
            seePaySum:function () {
                var formData = form.val('pay-order-sta')
                $.get('/admin/payOrder/sta',formData,function (res) {
                    if (res.code!==0) {
                        layer.msg(res.msg,{icon:2})
                        return;
                    }
                    _this.paySum = res.data.paySum +"元"
                })
            }
        },
        created:function(){
            _this = this;
        },
        mounted:function () {
            var _this = this;

            layui.use(['form', 'table','laydate'], function () {
                form = layui.form;
                $ = layui.jquery;
                var isLodedTable = false;

                var table = layui.table;
                var laydate=layui.laydate;

                laydate.render({
                    elem: '#createTime0',
                    type:'datetime',
                    value: getYmd(0)+ " 00:00:00"
                });
                laydate.render({
                    elem: '#createTime1',
                    type:'datetime',
                    value: getYmd(1) + " 00:00:00"
                });
                laydate.render({
                    elem: '#payTimeBegin',
                    type:'datetime',
                    value: getYmd(0)+ " 00:00:00"
                });
                laydate.render({
                    elem: '#payTimeEnd',
                    type:'datetime',
                    value: getYmd(1) + " 00:00:00"
                });
                //商品列表选择
                $.get('/admin/gameGoods/paginate',{limit:9999999,channeltype:0},function (res) {
                    _this.gameGoodsList = res.data;
                    form.render();
                })//默认官方渠道商品

                form.render();
                form.on('select(setGemValue)',function(data){ //
                    console.log('选择了商品配置类型',data.value)
                    var selectId = data.value;


                        $.get('/admin/gameGoods/paginate',{limit:9999999,channeltype:selectId},function (res) {
                            _this.gameGoodsList = res.data;
                            form.render(); // 在数据更新后再渲染

                        })
                })

                //包列表
                $.get("/admin/gamePackage/paginate",{page:1,limit:9999},function(res){
                    _this.packages = res.data;
                    _this.packages1 = res.data;

                    console.log('选择了商品配置类型1111',_this.packages)

                    _this.updatePackeages(_this.packages); // 初始渲染区服列表选择器
                    _this.updatePackeages1(_this.packages1); // 初始渲染区服列表选择器

                });


                form.render();

                // 更新游戏区选择器
                _this.updatePackeages= function (Packeages) {
                    var datapackeages = [];
                    for (var k in Packeages) {
                        var v = Packeages[k];
                        datapackeages.push({
                            name: v.package_no,
                            value: v.package_no,
                        });
                    }
                    // 渲染游戏区选择器
                    xmSelect.render({
                        el: '#serverPackage',
                        name: "serverPackage",
                        language: 'zn',
                        data: datapackeages,
                        multiple: true,// 确保开启多选
                    });
                };

                // 更新游戏区选择器
                _this.updatePackeages1= function (Packeages) {
                    var datapackeages1 = [];
                    for (var k in Packeages) {
                        var v = Packeages[k];
                        datapackeages1.push({
                            name: v.package_no,
                            value: v.package_no,
                        });
                    }
                    // 渲染游戏区选择器
                    xmSelect.render({
                        el: '#serverPackage1',
                        name: "serverPackage1",
                        language: 'zn',
                        data: datapackeages1,
                        multiple: true,// 确保开启多选
                    });
                };



                // 更新游戏区选择器
                _this.updateGameArea = function (services) {
                    var dataServers = [];
                    for (var k in services) {
                        var v = services[k];
                        dataServers.push({
                            name: v.name,
                            value: v.server_id,
                        });
                    }
                    // 渲染游戏区选择器
                    xmSelect.render({
                        el: '#gameAreas',
                        name: "gameAreas",
                        language: 'zn',
                        data: dataServers,
                        multiple: true,// 确保开启多选
                    });
                };




                var dataSg = [];
                for(var k in serverGroup){
                    var v = serverGroup[k];
                    dataSg.push({
                        name:v.group_name,
                        value:v.group_id,
                    })
                }

                xmSelect.render({
                    el: '#serverGroup',
                    name:"serverGroup",
                    language: 'zn',
                    data: dataSg,
                    on: function(data){
                        // 选择器值发生变化时触发
                        console.log('DATA:', data);
                        var selectedValues = data.arr.map(item => item.value);
                        // 在这里编写您想要执行的方法
                        handleServerGroupChange(selectedValues);
                    }
                })

                _this.updateGameArea(services); // 初始渲染区服列表选择器

                function handleServerGroupChange(selectedValues) {
                    // 在这里编写您想要执行的逻辑
                    if (selectedValues.length === 0) {
                        // 如果 selectedValues 为空数组,则不发送请求
                        console.log('No server groups selected.');
                        return;
                    }
                    console.log('Selected server groups:', selectedValues);
                    var url = '/admin/server/serverList';
                    var params = { groups: selectedValues };
                    var paramsString = $.param(params);
                    $.get(url + '?' + paramsString, function(res) {
                        console.log('res:', res.data);
                        _this.$set(_this, 'services', res.data);
                        _this.$nextTick(function() {
                            form.render();
                        });
                    });
                }

                // 监听代理渠道选择器的变化
                form.on('select(gamechannel)', function(data) {
                    var channelcg = data.value; // 获取选择的渠道ID
                    if (channelcg) {
                        $.get("/admin/gamePackage/paginate",{page:1,limit:9999,channelcg:channelcg},function(res){
                            _this.packages = res.data;
                            _this.updatePackeages(_this.packages); // 初始渲染区服列表选择器
                        });
                    }
                });
                // 监听代理渠道选择器的变化
                form.on('select(gamechannel1)', function(data) {
                    var channelcg = data.value; // 获取选择的渠道ID
                    if (channelcg) {
                        $.get("/admin/gamePackage/paginate",{page:1,limit:9999,channelcg:channelcg},function(res){
                            _this.packages = res.data;
                            _this.updatePackeages1(_this.packages); // 初始渲染区服列表选择器
                        });
                    }
                });


                var loadTable = function(){


                    var defaultSearchValues = {
                        status: 1,
                        push_status: 1
                    };

                    // 初始化表单选择框的值
                    form.val('table-search', defaultSearchValues);
                    table.render({
                        where: form.val('table-search'),
                        elem: '#currentTableId',
                        url: '/admin/payOrder/paginate',
                        defaultToolbar: ['filter', 'print' ],
                        width:2300,
                        toolbar: '#toolbarDemo',
                        cols: [[
                            {title: '操作', minWidth: 350, toolbar: '#currentTableBar', align: "center"},
                            {title: '我司订单号',field:"order_no" ,width:203},
                            {title: '平台订单号',field:"channel_order_no" ,width:203},
                            {title: '长ID',field:"customer_id"},

                            {title: '订单名称',field:"name" },
                            {title: '详情',field:"detail" },
                            {title: '下单时间',width:250,field:"created_at",templet:function(row){
                                    return row.created_at
                                }},
                            {title: '订单状态',field:"status" ,templet:function(row){
                                    switch(row.status) {
                                        case 0: return '待支付';
                                        case 1: return '已支付';
                                        case 9: return '异常订单';

                                    }
                                }},
                            {title: '推送状态',field:"push_status" ,templet:function(row){
                                    switch(row.push_status) {
                                        case 0: return '待推送';
                                        case 1: return '推送成功';
                                        case 9: return '金额不匹配';
                                        case -1: return '推送失败';
                                    }
                                }},
                            {title: '退款状态',field:"refund_status" ,templet:function(row){
                                    switch(row.refund_status) {
                                        case 0: return '未退款';
                                        case 1: return '已退款';
                                        case null: return '未退款';
                                    }
                                }},
                            {title: '区服编号',field:"game_area" },
                            {title: '角色ID',field:"role_id" },
                            {title: '包号',field:"package_no" },
                            {title: '购买数量',field:"number" },
                            {title: '客户端传入价格',field:"front_fee" ,templet:function(row){
                                    return row.front_fee  + "元"
                                }},
                            {title: '真实价格',field:"fee" ,templet:function(row){
                                    return row.fee  + "元"
                                }},


                        ]],
                        limits: [10, 15, 20, 25, 50, 100],
                        limit: 15,
                        page: true,
                        skin: 'line'
                    });
                    isLodedTable = true;
                }
                loadTable();
                // 监听搜索操作
                form.on('submit(data-search-btn)', function (data) {
                    if (!isLodedTable){
                        loadTable();
                    } else {
                        //执行搜索重载
                        table.reload('currentTableId', {
                            page: {
                                curr: 1
                            }
                            , where: form.val('table-search')
                        }, 'data');
                    }
                    return false;
                });
                form.render()

                table.on('tool(currentTableFilter)', function (obj) {
                    var data = obj.data;
                    if (obj.event==='query'){
                        $.post('/admin/payOrder/'+data.id+'/query',{},function(res){
                            if (res.code === 0) {
                                layer.msg('success', {icon: 6, time: 500}, function () {
                                    //location.reload(); //父页面刷新
                                })
                            } else {
                                layer.msg(res.msg, {icon: 5})
                            }
                        })
                    } else if (obj.event === 'push') {
                        $.post('/admin/payOrder/'+data.id+'/push',{},function(res){
                            if (res.code === 0) {
                                layer.msg('success', {icon: 6, time: 500}, function () {
                                    location.reload(); //父页面刷新
                                })
                            } else {
                                layer.msg(res.msg, {icon: 5})
                            }
                        })
                    } else if(obj.event==='force-push'){
                        layer.confirm('确定要强制重新推送吗',{icon:3},function(){
                            $.post('/admin/payOrder/'+data.id+'/forcePush',{},function(res){
                                if (res.code === 0) {
                                    layer.msg('success', {icon: 6, time: 500}, function () {
                                        location.reload(); //父页面刷新
                                    })
                                } else {
                                    layer.msg(res.msg, {icon: 5})
                                }
                            })
                        })
                    } else if(obj.event==='nopay-force-push'){
                        layer.confirm('真的要给未付款用户推送吗?',{icon:3},function(){
                            $.post('/admin/payOrder/'+data.id+'/nopayForcePush',{},function(res){
                                if (res.code === 0) {
                                    layer.msg('success', {icon: 6, time: 500}, function () {
                                        location.reload(); //父页面刷新
                                    })
                                } else {
                                    layer.msg(res.msg, {icon: 5})
                                }
                            })
                        })
                    }else if(obj.event==='edit'){
                        var formData = {};
                        formData.refund_status = 1;
                        layer.confirm('标记已退款',{icon:3},function(){
                            $.post('/admin/payOrder/'+data.id+'/edit',formData,function(res){
                                if (res.code === 0) {
                                    layer.msg('success', {icon: 6, time: 500}, function () {
                                        location.reload(); //父页面刷新
                                    })
                                } else {
                                    layer.msg(res.msg, {icon: 5})
                                }
                            })
                        })
                    }
                });

                table.on('toolbar(currentTableFilter)', function (obj) {
                    if (obj.event === 'export') {
                        var searchData = form.val('table-search');
                        searchData.page = 1;
                        searchData.limit = 9999999;
                        var layerIndex = layer.msg("请求中请耐心等待...",{icon:16,time:999999999});
                        //layer.close(layerIndex)
                        $.get('/admin/payOrder/export',searchData,function(res){
                            layer.close(layerIndex)
                            browserDownload( "/" + res.data);
                        });

                    }
                });
            });
        }
    })

</script>
